package com.vhuomai.product.label.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Scope;
import com.vhuomai.product.label.entity.BaseLabel;
import com.vhuomai.product.label.service.LabelService;
import com.vhuomai.system.constant.UserRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Cui
 */
@Api(tags = "标签管理")
@RestController
@RequestMapping("/api")
@Scope(role = UserRole.MANAGER)
public class LabelController {
    @Autowired
    private LabelService labelService;

    @ApiOperation(value = "添加标签")
    @PostMapping(value = "/product/label", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> createLabel(@RequestBody List<BaseLabel> productLabels) {
        labelService.createLabel(productLabels);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "更新标签")
    @PutMapping(value = "/product/label", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateLabel(@RequestBody BaseLabel productLabel) {
        labelService.updateLabel(productLabel);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除标签")
    @DeleteMapping(value = "/product/label", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> deleteLabel(@RequestParam("id") Integer id) {
        labelService.deleteLabel(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "标签列表")
    @GetMapping(value = "/product/label", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<List<BaseLabel>> labelList(@RequestParam("categoryId") Integer categoryId) {
        return ServiceResult.success(labelService.findLabels(categoryId));
    }
}
